package com.vids.alarm;


import com.vids.mapper.ApeDataTotalMapper;
import com.vids.mapper.ApeMapper;
import com.vids.model.alarm.DataTotal;
import com.vids.model.entity.Ape;
import com.vids.model.param.FaceModel;
import com.vids.utils.AlarmUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;
import java.util.Map;

public class ApeTask implements Runnable {
    // map
    private Map<String, Long> map;
    // 存储当前天的时间 用来和每次获取的时间进行比对 如果大于当前天 则存储为新一天的时间
    private String endDayTime;
    private FaceModel faceModel;
    @Autowired
    private ApeMapper apeMapper ;
    @Autowired
    private ApeDataTotalMapper apeDataTotalMapper;
    private final  Object obj = new Object();


    public Map<String, Long> getMap() {
        return map;
    }

    public void setMap(Map<String, Long> map) {
        this.map = map;
    }

    public String getEndDayTime() {
        return endDayTime;
    }

    public void setEndDayTime(String endDayTime) {
        this.endDayTime = endDayTime;
    }

    public FaceModel getFaceModel() {
        return faceModel;
    }

    public void setFaceModel(FaceModel faceModel) {
        this.faceModel = faceModel;
    }


    public ApeTask(Map<String, Long> map, String endDayTime, FaceModel faceModel) {
        this.map = map;
        this.endDayTime = endDayTime;
        this.faceModel = faceModel;
    }

    @Override
    public void run() {

      // 首先判断是否是新的一天 第一次计数,如果是,将抓拍时间赋值给currentDayTime
        //if(map.get(apeMapper.findAll().get(0))==0 ){
        synchronized (obj){  // 同步代码块
            if(endDayTime==null||endDayTime.equals("")){
                endDayTime = AlarmUtils.getSpecifiedDayAfter(faceModel.getFaceAppearTime());

            }
            Date endTime = AlarmUtils.convert(endDayTime);
            Date zpsj = AlarmUtils.convert(faceModel.getFaceAppearTime());

            if(zpsj.getTime()<endTime.getTime()){
                // 如果抓拍时间 是当天，则计数加1
                Long aLong = map.get(faceModel.getDeviceID());
                aLong = aLong+1;
                map.put(faceModel.getDeviceID(),aLong);
            }else{
                // 数据插入到表中 设备id  当前日期  数量
                String currentDay = AlarmUtils.getSpecifiedDayBefore(endDayTime);
                for (Map.Entry<String, Long> entry : map.entrySet()) {
                    String key = entry.getKey();  // 设备id
                    Long value = entry.getValue();  // 数据量
                    DataTotal dataTotal = new DataTotal();
                    dataTotal.setDeviceId(key);
                    dataTotal.setTotal(value);
                    dataTotal.setTime(currentDay);
                    apeDataTotalMapper.insertDataTotal(dataTotal);

                }
                // 初始化map
                List<Ape> all = apeMapper.findAll();
                for (Ape ape:all
                ) {
                    // 将该设备的数据量 初始化为0
                    map.put(ape.getApeID(),(long)0);
                }
                // 初始化endDayTime
                endDayTime="";
                // 新的一天计数+1
                Long aLong = map.get(faceModel.getDeviceID());
                aLong = aLong+1;
                map.put(faceModel.getDeviceID(),aLong);
            }
        }


    }
}
